package rsschart.charter;

import rsschart.feed.RssChart;

public class CharterUtil {
	
	public final static int LINEAR_GRADIENT = 0;
	public final static int RADIAL_GRADIENT = 1;
	public final static int MAX_COLOR = 20;
		
	public static String clickNote() {
		return "<text x=\"5\" y=\"8\" text-anchor=\"start\" font-size=\"6\">Click a chart element...</text>\n";
	}

	public static String getHeader(int w, int h) {
		StringBuilder str = new StringBuilder();
		str.append("<?xml version=\"1.0\" standalone=\"no\"?>");
		str.append("\n");
		str.append("<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\" " +
				"\n"+
				"\"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n");
		str.append("<svg version=\"1.1\" xmlns=\"http://www.w3.org/2000/svg\" " +
				"xmlns:xlink=\"http://www.w3.org/1999/xlink\" " +
				"width=\"" + w + " px\"  height=\"" + h + "px\" viewBox=\"0 0 320 240\">\n");

		return str.toString();
	}
	
	public static String getClose() {
		return "\n</svg>";
	}

	//-- font size fixed at 11 point:
	public static String getTitle(int x, int y, String title) {
		StringBuilder str = new StringBuilder();
		str.append("<text x=\""+ x + "\" y=\"" + y + "\"");
		str.append(" text-anchor=\"middle\" font-size=\"11\">" + title + "</text>\n");
		return str.toString();
	}
	
	public static String getChartFrame(int x, int y, int width, int height) {
		String frame;
		frame = "<rect x=\"" + x + "\" y=\"" + y + "\" width=\"" + width +
				"\" style=\"fill:url(#background-grad)\"" +
				" height=\"" + height + "\" fill=\"none\" stroke=\"black\" stroke-width=\"0.25\"/>\n";
		return frame;
	}

	
	public static String getClickMethod() {
		StringBuffer method = new StringBuffer();
		
		method.append("<script type=\"text/javascript\">\n");
		method.append("<![CDATA[\n");
		method.append("function doLink(rssLink, catg, catgVal, dataSet, dataSetVal, val) {\n");
		method.append("    var e_catg = escape(catg);\n");
		method.append("    var e_catgVal = escape(catgVal);\n");
		method.append("    var e_dataSet = escape(dataSet);\n");
		method.append("    var e_dataSetVal = escape(dataSetVal);\n");
		method.append("    var e_val = escape(val);\n");
		method.append("    var fullLink = rssLink + \"?catg=\" + e_catg + \"&catgVal=\" + e_catgVal + ");
		method.append("\"&dataSet=\" + e_dataSet + \"&dataSetVal=\" + e_dataSetVal + \"&val=\" + e_val;\n");
		method.append("    var w = window.open();\n");
		method.append("    w.document.location=fullLink;\n");
		method.append("}\n");
		method.append("//]]>\n");
		method.append("</script>\n");
		
		return method.toString();

	}

	public static String goToSite() {
		StringBuffer method = new StringBuffer();
		
		method.append("<script type=\"text/javascript\">\n");
		method.append("<![CDATA[\n");
		method.append("function goToSite() {\n");
		method.append("    var fullLink = 'http://www.iguanatronics.com';\n");
		method.append("    var w = window.open();\n");
		method.append("    w.document.location=fullLink;\n");
		method.append("}\n");
		method.append("//]]>\n");
		method.append("</script>\n");
		
		method.append("<text x=\"5\" y=\"225\" text-anchor=\"start\" font-size=\"5\" cursor=\"pointer\" onclick=\"goToSite()\">iguanatronics.com</text>\n");
		
		return method.toString();

	}

	
	
	public static String getDoLink(String methodName, String url, String catg, String catgVal, 
								   String dataSet, String dataSetVal, String val) {
		String str;
		
		str = methodName + "('" + url + "','" + catg + "','" + catgVal + "','" + dataSet + 
			  "','" + dataSetVal + "','" + val + "')";
		return str;

	}

	/**
	 * Array of colors for pie slice, multi bars and lines
	 * @param i
	 * @return Color in #rrggbb hex
	 */
	public static String c(int i) {
		String[] color;
		color = new String[20];
		color[0] = "#820f0c";
		color[1] = "#54507e";
		color[2] = "#4c765a";
		color[3] = "#864E88";
		color[4] = "#d4d64a";
		color[5] = "#4EC1C8";
		color[6] = "#DFD42F";
		color[7] = "#6C78AA";
		color[8] = "#1D19BD";
		color[9] = "#928984";
		color[10] = "#BC1A1A";
		color[11] = "#6f5f64";
		color[12] = "#0eb0ba";
		color[13] = "#dca206";
		color[14] = "#70A67F";
		color[15] = "#498766";
		color[16] = "#f8c40e";
		color[17] = "#7f376d";
		color[18] = "#58973F";
		color[19] = "#edbe87";
		return color[i];
		
	}

	public static String getGradient(int count, int gradientType) {
		StringBuffer gradient = new StringBuffer();
		
		String color;
		String midColor;
		long midColorHex;
		int colorIndex;
		int r;
		int g;
		int b;
		String hexPart;

		gradient.append("<defs>");
		
		//-- add bar chart gradient
		gradient.append("<linearGradient id=\"background-grad\" x1=\"0%\" y1=\"0%\" x2=\"100%\" y2=\"0%\">\n");
		gradient.append("<stop offset=\"0%\" style=\"stop-color:#dedede;stop-opacity:1\"/>\n");
		gradient.append("<stop offset=\"100%\" style=\"stop-color:#ffffff;stop-opacity:1\"/>\n");
		gradient.append("</linearGradient>\n");
		
		colorIndex=0;
		for (int i=0;i<count;i++) {
			color = CharterUtil.c(colorIndex);
			
			if (gradientType == LINEAR_GRADIENT) {
				//midColorHex = Long.decode("0x" + color.substring(1)) + 0x555555;
				//-- use this formula to find the "highlight" color: add 2/3 the difference between 0xff and the color per RGB portion:
				hexPart = color.substring(1,3);
				r = Integer.decode("0x" + hexPart) + 0x55;
				r = r > 0xee?0xee:r;
				hexPart = color.substring(3,5);
				g = Integer.decode("0x" + hexPart) + 0x55;
				g = g > 0xee?0xee:g;
				hexPart = color.substring(5);
				b = Integer.decode("0x" + hexPart) + 0x55;;
				b = b > 0xee?0xee:b;
				
				//midColorHex = midColorHex >= 0xffffff ? 0xeeeeee : midColorHex;
				//-- don't know how this will handle single digit portions of the RGB...
				midColorHex = Long.decode("0x" + Integer.toHexString(r) + Integer.toHexString(g) + Integer.toHexString(b));
				midColor = "#" + Long.toHexString(midColorHex);
				
				gradient.append("<linearGradient id=\"c" + colorIndex + "\" x1=\"0%\" y1=\"0%\" x2=\"100%\" y2=\"0%\">\n");
				gradient.append("<stop offset=\"0%\" style=\"stop-color:" + color + ";stop-opacity:1\"/>\n");
				gradient.append("<stop offset=\"65%\" style=\"stop-color:" +  midColor+ ";stop-opacity:1\"/>\n");
				gradient.append("<stop offset=\"100%\" style=\"stop-color:" + color + ";stop-opacity:1\"/>\n");
				gradient.append("</linearGradient>\n");
			}
			else {
				gradient.append("<radialGradient id=\"c" + colorIndex + "\">\n");
				gradient.append("<stop offset=\"0%\" stop-color=\"" + color + "\" stop-opacity=\"0.4\"/>\n");
				gradient.append("<stop offset=\"100%\" stop-color=\"" + color + "\" stop-opacity=\"1\"/>\n");
				gradient.append("</radialGradient>\n");
			}
			
			colorIndex++;
			//-- reset to starting color:
			if (colorIndex==MAX_COLOR) {colorIndex=0;}
		}
		gradient.append("</defs>");
		
		return gradient.toString();
	}
	
}
